import gui.PitchGUI
import ai.AIEngine
import dao.DataLoader
import engine.GameEngine

/**
 * 
 */

/**
 * The main execution class.
 * 
 * @author Tomasz Dąbrowski
 *
 */
class Main {

	RestartThread rt

	public static void main(String[] args) {
		//		try {
		//			PitchGUI.createInstance()
		//			PitchGUI.getInstance().disp
		//			GameEngine.createInstance().startNewGame(DataLoader.getInstance().getTeamA(), DataLoader.getInstance().getTeamB());
		//		} catch (Exception ex) {
		//			System.out.println("An exception has been raised: " + ex.getMessage());
		//			ex.printStackTrace();
		//		}
		Main main = new Main()
		main.rt = new RestartThread()
		main.rt.start()
	}
}

class RestartThread extends Thread {

	/* (non-Javadoc)
	 * @see java.lang.Thread#run()
	 */
	@Override
	public void run() {
		while(true) {
			synchronized (AIEngine.threadsLock) {
				PitchGUI.createInstance()
				GameEngine.createInstance().startNewGame(DataLoader.getInstance().getTeamA(), DataLoader.getInstance().getTeamB());
				AIEngine.threadsLock.notifyAll()
				long time = System.currentTimeMillis()
				while(System.currentTimeMillis() - time < 1000 * 60 * 10 && !AIEngine.killFlag) {
					AIEngine.threadsLock.wait()
				}
				System.out.println("restarting...");
				AIEngine.killFlag = true
				time = System.currentTimeMillis()
				while(System.currentTimeMillis() - time < 1000) {
					AIEngine.threadsLock.wait(100)
				}
				PitchGUI.getInstance().dispose()
				AIEngine.killFlag = false
				System.out.println("restarted");
			}
		}
	}
}
